const router = require('koa-router')()
// import { User } from '../modal/User';
const User = require('../service/user');
const tt = require('../modal/Test');
const uuid = require('node-uuid');

router.get('/', async (ctx, next) => {
  let res = await tt.findAll();
  ctx.body = {
    title: uuid.v4()
  };
})

router.get('/regist', async (ctx, next) => {
  let userInfo = {
    fLoginName: 'www',
    fNickname: 'www',
    fName: 'lc',
    fPassword: 'www',
    fPhoneNumber: 'wwww',
    fPic: '111'
  };
  await User.create({
    ...userInfo,
    fid: uuid.v4()
  });
  //ctx.body = 'koa2 string'
})

router.post('/login', async (ctx, next) => {
  let param = ctx.request.body;
  if(!param) {
    ctx.body = {
      data: null,
      success: false,
      msg: '参数不能为空'
    }
    return;
  }
  if(param.fLoginName === null || param.fLoginName === '') {
    ctx.body = {
      data: null,
      success: false,
      msg: '用户名不能为空'
    }
    return;
  }
  if(param.fPassword === null || param.fPassword === '') {
    ctx.body = {
      data: null,
      success: false,
      msg: '密码不能为空'
    }
    return;
  }
  let res = await User.login(param);
  ctx.body = res;
})

router.get('/getMenu', async (ctx, next) => {
  let res = await User.getMenu();
  ctx.body = res;
})

module.exports = router
